/*************************************************************
 * @Author: dENGcj                                           *
 * @Version: 0.1.b20210102                                   *
 * @Date: 2020-12-31 23:11                                   *
 * @LastEditTime: 2021-01-02 23:02                           *
 * @Descripttion: Implementation of AD9833 Driver.           *
 *************************************************************/

/*************************************************************
 *         Warning: Read AD9833 Datasheet First.             *
 *************************************************************/

/*************************************************************
 * Uncomment the macro below to add uint16_t and uint32_t    *
 *  type support.                                            *
 *************************************************************/
#include <inttypes.h>

/*************************************************************
 * Define the MCLK frequency in MHz.                         *
 *************************************************************/
#define MCLK 20

/*************************************************************
 * Define the bits in control register.Function Specified.   *
 *************************************************************/

#define CMD_WRITE_LSB_MSB ((0 << 15) | (1 << 13))
#define CMD_WRITE_MSB ((0 << 15) | (0 << 13) | (1 << 12))
#define CMD_WRITE_LSB ((0 << 15) | (0 << 13) | (0 << 12))
#define CMD_SELECT_FREQ0 ((0 << 15) | (0 << 11))
#define CMD_SELECT_FREQ1 ((0 << 15) | (1 << 11))
#define CMD_SELECT_PHASE0 ((0 << 15) | (0 << 10))
#define CMD_SELECT_PHASE1 ((0 << 15) | (1 << 10))
#define CMD_RESET ((0 << 15) | (1 << 8))
#define CMD_CLEAR_RESET ((0 << 15) | (0 << 8))

#define CMD_DISABLE_MCLK ((0 << 15) | (1 << 7))
#define CMD_ENABLE_MCLK ((0 << 15) | (0 << 7))
#define CMD_POWER_OFF_DAC ((0 << 15) | (1 << 6))
#define CMD_POWER_ON_DAC ((0 << 15) | (0 << 6))

#define CMD_OUTPUT_MSB ((0 << 15) | (1 << 5) | (1 << 3))
#define CMD_OUTPUT_MSB_DIV2 ((0 << 15) | (1 << 5) | (0 << 3))
#define CMD_OUTPUT_TRIANGLE ((0 << 15) | (1 << 1))
#define CMD_OUTPUT_SINUSOIDAL ((0 << 15) | (0 << 1))

/*************************************************************
 * Define which register to write.Bitwise OR with FreqValue- *
 *  or PhaseValue-.AD9833 has two frequency registers and two*
 *  phase registers.                                         *
 *************************************************************/

#define REG_WRITE_FREQ0 ((0 << 15) | (1 << 14))
#define REG_WRITE_FREQ1 ((1 << 15) | (0 << 14))
#define REG_WRITE_PHASE0 ((1 << 15) | (1 << 14) | (0 << 13))
#define REG_WRITE_PHASE1 ((1 << 15) | (1 << 14) | (1 << 13))

void chilis9833_Init(void);                                                     /* Init AD9833. */
void chilis9833_Reset(void);                                                    /* Reset AD9833. */
void chilis9833_Clear_Reset(void);                                              /* Disable reset. */
void chilis9833_Write_Freq_2Bytes(uint16_t freqX, uint32_t freqValue);          /* Set Frequency of porper Freq Register(28bit).Both MSB(14bit) and LSB(14bit). */
void chilis9833_Write_Freq_MSB(uint16_t freqX, uint32_t freqValue);             /* Set MSB(14bit) of porper Freq Register(28bit). */
void chilis9833_Write_Freq_LSB(uint16_t freqX, uint32_t freqValue);             /* Set LSB(14bit) of porper Freq Register(28bit). */
void chilis9833_Write_Phase(uint16_t phaseX, uint16_t phaseValue);              /* Set Phase of porper Phase Register(12bit). */
void chilis9833_Output_Mode(uint16_t freqx, uint16_t phasex, uint16_t outMode); /* Set the output of AD9833. */
void chilis9833_Set_Control_Bits(uint16_t ctrlBit);                             /* Set control bits of AD9833. */